﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;


namespace lxwmvc
{
    /// <summary>
    /// 功能:Cookies基本存取操作
    /// [09.4.10 lxw]<para />
    /// </summary>
    public class cookieshelper 
    {
        /// <summary>
        /// 功能:cookies的前缀
        /// [11-03-09 09:16  周三 lxw]<para />
        /// </summary>
        const string cookiesqianzhun = "-lxw-";


        /// <summary>
        /// 功能:设置Cookies对应键的值
        /// [09.1.24 lxw]<para />
        /// </summary>
        /// <param name="strcookieskey">键</param>
        /// <param name="strcookiesvalue">值</param>
        /// <param name="domain"></param>
        /// <param name="expiressecond">以秒作为单位，默认永远不过期</param>
        public static void setcookie(string strcookieskey, string strcookiesvalue, string domain = "", double expiressecond = 0)
        {

            string strHost = HttpContext.Current.Request.Url.Host;
            if (!string.IsNullOrEmpty(domain)) strHost = domain;
            string strpre = strHost + "_" + HttpContext.Current.Request.Url.Port;
            strpre = strpre.Replace(".", "_");

            strcookieskey = cookiesqianzhun + strpre + strcookieskey;
            HttpCookie cookies = new HttpCookie(strcookieskey,
                HttpUtility.UrlEncode(cryptohelper.DESEncrypt(strcookiesvalue,key))
                );

            if (domain.Length > 0)
            {
                cookies.Domain = domain;
                cookies.Secure = false;
            }

            //lxw 2013 11 11
            //让用户必须登录系统
            if (expiressecond > 0)
                cookies.Expires = DateTime.Now.AddSeconds(expiressecond);
            else
                cookies.Expires = DateTime.Now.AddYears(1);

            HttpContext.Current.Response.Cookies.Remove(cookies.Name);
            HttpContext.Current.Response.SetCookie(cookies);
        }

        static readonly string key = "lxw_2015";
        /// <summary>
        /// 功能:获取Cookies对应键的值
        /// [09.1.24 lxw]<para />
        /// </summary>
        /// <param name="strcookieskey">键</param>
        /// <param name="domain"></param>
        /// <returns>返回指定键的值</returns>
        public static string getcookie(string strcookieskey,string domain = "")
        {
            string strHost = HttpContext.Current.Request.Url.Host;
            if (!string.IsNullOrEmpty(domain)) strHost = domain;

            string strpre = strHost + "_" + HttpContext.Current.Request.Url.Port;
            strpre = strpre.Replace(".", "_");

            strcookieskey = cookiesqianzhun + strpre + strcookieskey;

            if (HttpContext.Current.Request.Cookies[strcookieskey] != null)
            {
                //url地址编码存储
                return cryptohelper.DESDecrypt(HttpUtility.UrlDecode(                    
                    System.Web.HttpContext.Current.Request.Cookies[strcookieskey].Value)
                    , key);
            }

            return "";
        }



        /// <summary>
        /// 功能:删除cookies
        /// [11-03-09 09:15  周三 lxw]<para />
        /// </summary>
        /// <param name="strcookieskey">key</param>
        /// <param name="domain">domain</param>
        public static void delcookie(string strcookieskey, string domain = "")
        {
            //string strHost = HttpContext.Current.Request.Url.Host;
            //if (!string.IsNullOrEmpty(domain)) strHost = domain;
            //string strpre = strHost + "_" + HttpContext.Current.Request.Url.Port;
            //strpre = strpre.Replace(".", "_");

            //strcookieskey = cookiesqianzhun + strpre + strcookieskey;
            //HttpContext.Current.Response.Cookies.Remove(strcookieskey);    

            //立刻过期
            setcookie(strcookieskey, "-1", domain, 1);
        }
    }
}
